package com.glink.common.serializer;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.glink.common.util.BaseCommonUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.Objects;

/**
 * yyyy-MM-dd HH:mm
 * Created by rencl on 2017/12/27.
 */
public class JsonDate4DeSerializer extends JsonDeserializer<Date> {
    @Override
    public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) {
        try {
            if (Objects.nonNull(jsonParser.getText()) && StringUtils.length(jsonParser.getText()) > 0) {
                return BaseCommonUtils.DATE_FORMAT_DAY_5.parse(jsonParser.getText());
            } else {
                return null;
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            throw new RuntimeException(e);
        }
    }
}
